package wymiana.service;

import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wymiana.dao.GroupRequestDao;
import wymiana.domain.Department;
import wymiana.domain.GroupRequest;
import wymiana.domain.User;

@Service("groupRequestService")
@Transactional
public class GroupRequestService {

	@Autowired
	private GroupRequestDao groupRequestDao;

	public boolean create(GroupRequest d) {
		boolean added = groupRequestDao.checkIfAdded(d);
		if (added) {
			return added;
		}
		groupRequestDao.save(d);
		return false;

	}

	public List<GroupRequest> getAllDepartments() {
		return groupRequestDao.getAll();
	}

	public GroupRequest getGroupRequest(int p_id) {
		GroupRequest d = groupRequestDao.getGroupRequestById(p_id);
		try {
			Hibernate.initialize(d.getAskingUser());
			Hibernate.initialize(d.getDgroup());
		} catch (Exception e) {

		}

		return d;
	}

	public void updateDepartment(GroupRequest d) {
		groupRequestDao.merge(d);
	}

	public void deleteGroupRequestById(int id) {
		GroupRequest d = groupRequestDao.getGroupRequestById(id);
		groupRequestDao.delete(d);
	}

	public List<GroupRequest> getMyGroupRequest(User u) {
		return groupRequestDao.getMyRequests(u);
	}

}
